package web

import (
	"jgt-oper/src/bll"
	"jgt-oper/src/context"
	"jgt-oper/src/schema"
	"jgt-oper/src/util"
	"strings"
)

// UserType 用户类型管理
type UserType struct {
	UserTypeBll *bll.UserType `inject:""`
}

// QueryPage 查询分页数据
func (a *UserType) QueryPage(ctx *context.Context) error {
	pageIndex, pageSize := ctx.GetPageIndex(), ctx.GetPageSize()

	params := schema.UserTypeQueryParam{
		Name:   ctx.Query("name"),
		Status: util.S(ctx.Query("status")).Int(),
	}

	total, items, err := a.UserTypeBll.QueryPage(params, pageIndex, pageSize)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	result := map[string]interface{}{
		"list": items,
		"pagination": map[string]interface{}{
			"total":    total,
			"current":  pageIndex,
			"pageSize": pageSize,
		},
	}

	return ctx.ResSuccess(result)
}

// QuerySelect 查询选择数据
func (a *UserType) QuerySelect(ctx *context.Context) error {
	var params schema.UserTypeSelectParam

	items, err := a.UserTypeBll.QuerySelect(params)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResSuccess(items)
}

// Get 精确查询数据
func (a *UserType) Get(ctx *context.Context) error {
	item, err := a.UserTypeBll.Get(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResSuccess(item)
}

// Create 创建数据
func (a *UserType) Create(ctx *context.Context) error {
	ctx.WriteOperateLog("UserType.Create", "用户类型管理-创建数据")
	var item schema.UserType
	if err := ctx.ParseJSON(&item); err != nil {
		return ctx.ResErrBadRequest(err)
	}

	item.Creator = ctx.GetUserID()
	err := a.UserTypeBll.Create(&item)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// Update 更新数据
func (a *UserType) Update(ctx *context.Context) error {
	ctx.WriteOperateLog("UserType.Update", "用户类型管理-更新数据")
	var item schema.UserType
	if err := ctx.ParseJSON(&item); err != nil {
		return ctx.ResErrBadRequest(err)
	}

	err := a.UserTypeBll.Update(ctx.Param("id"), &item)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// DeleteMany 删除多条数据
func (a *UserType) DeleteMany(ctx *context.Context) error {
	ctx.WriteOperateLog("UserType.DeleteMany", "用户类型管理-删除多条数据")
	ids := strings.Split(ctx.Query("batch"), ",")

	for _, id := range ids {
		err := a.UserTypeBll.Delete(id)
		if err != nil {
			return ctx.ResErrInternalServer(err)
		}
	}

	return ctx.ResOK()
}

// Delete 删除数据
func (a *UserType) Delete(ctx *context.Context) error {
	ctx.WriteOperateLog("UserType.Delete", "用户类型管理-删除数据")
	err := a.UserTypeBll.Delete(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// Enable 启用数据
func (a *UserType) Enable(ctx *context.Context) error {
	ctx.WriteOperateLog("UserType.Enable", "用户类型管理-启用数据")
	err := a.UserTypeBll.UpdateStatus(ctx.Param("id"), 1)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// Disable 禁用数据
func (a *UserType) Disable(ctx *context.Context) error {
	ctx.WriteOperateLog("UserType.Disable", "用户类型管理-禁用数据")
	err := a.UserTypeBll.UpdateStatus(ctx.Param("id"), 2)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// QueryAll 查询全部用户类型
func (a *UserType) QueryAll(ctx *context.Context) error {
	ps := new(schema.UserTypeQueryParam)
	ps.Status = 1
	data, err := a.UserTypeBll.QueryAll(*ps)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResSuccess(data)
}
